<?php
/**
 * Ticket
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     james.huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

include_once 'TaskConfigure.php';

class Ticket extends Zend_Db_Table
{	
	public $_db = null;
	public $_config = null;
	public $_filter_list =array();
	public $_entities = array();
	
	public function __construct () {
		$this->_db = Zend_Registry :: get('db_adapter');
		$this->_name = 'tt_ticket';
		$this->_primary = 'id';
		parent::_setup();
		// get configure entities
		$this->_config = new TaskConfigure();
		$this->_entities = $this->_config->_init();
	}
	
	public function setFilter ($filter_list = array()) {
		$this->_filter_list = $filter_list;
	}
	
	public function getFilter ($prefix = '') {
		if (sizeof($this->_filter_list) <= 0) return '';
		$filter_arr = array();
		foreach ($this->_filter_list as $k => $v) {
			if (strlen($v) <= 0 || (is_numeric($v) && $v <= 0)) continue;
			if ($k == 'title') {
				array_push($filter_arr, $k . ' like \'%' . $v . '%\'');
			} else {
				array_push($filter_arr, $k . ' = ' . $v);
			}
		}
		if (sizeof($filter_arr) > 0) {
			return $prefix.implode(' and ', $filter_arr);
		} else {
			return '';
		}
	}
	
	public function update (array $data, $id) {
		$where = $this->_db->quoteInto('id = ?', $id);
		return parent::update($data, $where);
	}
	
	public function getCols () {
		return $this->_cols;
	}
	
	public function getTotal () {
		$sql  = 'select count(*) from '.$this->_name;
		return $this->_db->fetchOne($sql);
	}
	
	public function getEntities ($type = null) {
		if ($type) {
			return $this->_entities[$type];
		}
		return $this->_entities;
	}

	public function getEntityValue ($type = null, $key = null) {
		if ($type) {
			$entity = $this->_entities[$type];
			if ($key) {
				return $entity[$key];
			}
		}
		return '--';
	}
	
	public function getTicketList () {
		try{
			$where  = $this->_db->quoteInto('id > (?)', 0);
			$where .= $this->getFilter(' and ');
			$tickets = $this->fetchAll($where, 'update_time desc, id');
			if ($tickets->count()) {
//				Utils::dumper($tickets->toArray());
				return $tickets->toArray();
			}
		} catch (Zend_Db_Table_Exception $e) {
			echo $e->getMessage();
		}
		return array();
	}

}
?>